const path = require("path");
const fs = require("fs");
const express = require("express");
const app = express();
const {createLog,isToken} = require("./modules/middleware");
const Path = require("path");
let tokenList = ["zhangsan","lisi"]
app.use(createLog(path.join(__dirname,"/data/index.json")));
app.get("/",(req,res)=>{
	res.send("首页")
})
app.get("/goodsList",(req,res)=>{
	
	res.send("商品列表页")
})
app.get("/goodsDetail",(req,res)=>{
	res.send("商品详情页")
})
app.get("/newsList",(req,res)=>{
	res.send("新闻列表页")
})
app.get("/newsDetail",(req,res)=>{
	res.send("新闻详情页")
})

app.get("/login",(req,res)=>{
	res.send("个人登陆")
})
// app.use((req,res,next)=>{
// 	if(tokenList.includes(req.query.token)){
// 		next();
// 	}else{
// 		res.redirect("/login")
// 	}
// })
app.get("/my",isToken(),(req,res)=>{
	// if(tokenList.includes(req.query.token))
	// 	res.send("个人中心")
	// else
	// 	res.redirect("/login");
	res.send("个人中心")
})
app.get("/myLog",isToken("/"),(req,res)=>{
	fs.readFile(path.join(__dirname,"/data/index.json"),"utf-8",(err,logList)=>{
		logList = JSON.parse(logList).filter(item=>item.ip === req.socket.remoteAddress)
			.map(item=>(`
				<div>
					<p>id:${item.id}</p>
					<p>访问时间:${item.addTime}</p>
					<p>请求方式:${item.method}</p>
					<p>请求地址:${item.url}</p>
					<p>IP:${item.ip}</p>
				</div>
				<hr/>
			`))
		res.send((`
			<div>
				<h3>您的访问日志：</h3>
				${logList.join("")}
			</div>
		`))
	})
})
app.listen(80,"0.0.0.0",()=>{
	console.log("success");
})